package jflowmap.ui.export;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import jflowmap.export.Exporter;
import jflowmap.export.Renderer;
import jflowmap.ui.export.decorators.FileChooserDecorator;

import net.java.dev.designgridlayout.DesignGridLayout;
import edu.umd.cs.piccolo.PCanvas;

public class ExporterWindow extends JFrame implements IExporterWindow {
	private static final long serialVersionUID = 1L;
	
	private Exporter exporter;
	private Renderer renderer;
	private final DesignGridLayout layout;
	private final DesignGridLayout quickLayout;
	private final DesignGridLayout moreLayout;
	private final DesignGridLayout exportLayout;
	
	private final JPanel quickOptions;
	private final JPanel moreOptions;
	private final JPanel exportOptions;
	private final PCanvas previewCanvas;
	
	private ExporterWindow (Exporter exporter, Renderer renderer) {
		super("Exportieren");
		
		this.setLayout(new BorderLayout());
		
		this.layout = new DesignGridLayout(this);
		this.exporter = exporter;
		this.renderer = renderer;
		
		this.previewCanvas = new PCanvas();
		
		this.quickOptions = new JPanel();
		this.quickLayout = new DesignGridLayout(this.quickOptions);
		this.moreOptions = new JPanel();
		this.moreLayout = new DesignGridLayout(this.moreOptions);
		this.exportOptions = new JPanel();
		this.exportLayout = new DesignGridLayout(this.exportOptions);
		
		this.previewCanvas.setBackground(Color.green);
		this.previewCanvas.setPreferredSize(new Dimension(700, 500));

		quickOptions.setBackground(Color.red);
		moreOptions.setBackground(Color.blue);
		exportOptions.setBackground(Color.yellow);

		layout.row().grid().add(previewCanvas, 4).add(quickOptions);
		layout.row().grid().spanRow().add(moreOptions);
		layout.row().grid().add(exportOptions, 2);
		
		quickLayout.row().grid().add(new JLabel("Optionen"));
		moreLayout.row().grid().add(new JLabel("Mehr"));
		exportLayout.row().left().add(new JLabel("Exportieren"), new JSeparator()).fill();
				
		this.pack();
		this.setResizable(false);
	}
	
	public static IExporterWindow create (Exporter exporter, Renderer renderer) {
		ExporterWindow w = new ExporterWindow(exporter, renderer);
		w.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		return w;
	}
	
	public static void main(String[] args) {
		IExporterWindow window = ExporterWindow.create(null, null);
		window.setVisible(true);
	}
	
	public final Exporter getExporter() {
		return this.exporter;
	}
	
	public final Renderer getRenderer() {
		return this.renderer;
	}

	@Override
	public Frame getFrame() {
		return this;
	}

	@Override
	public PCanvas getPreviewCanvas() {
		return this.previewCanvas;
	}

	@Override
	public DesignGridLayout getQuickOptionsGrid() {
		return this.quickLayout;
	}

	@Override
	public DesignGridLayout getButtonsGrid() {
		return this.moreLayout;
	}

	@Override
	public DesignGridLayout getExportOptionsGrid() {
		return this.exportLayout;
	}

	@Override
	public File getExportFile() {
		throw new UnsupportedOperationException("This exporter does not handle files!");
	}

	@Override
	public void export() {
		this.renderer.render(this.exporter);
	}

	@Override
	public void close() {
		this.dispose();
	}

	@Override
	public void setRenderer(Renderer renderer) {
		this.renderer = renderer;		
	}

	@Override
	public void setExporter(Exporter exporter) {
		this.exporter = exporter;
	}	
}
